Passed
Push — master ( 6bc43d...2da2ee )
by Ahmad
07:04
created

room.js ➔ displaySharedUsers   A

Complexity

Conditions 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 21
rs 9.75
c 0
b 0
f 0
eloc 13
1
// BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
2
//
3
// Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
4
//
5
// This program is free software; you can redistribute it and/or modify it under the
6
// terms of the GNU Lesser General Public License as published by the Free Software
7
// Foundation; either version 3.0 of the License, or (at your option) any later
8
// version.
9
//
10
// BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
12
// PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public License along
15
// with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
16
17
// Room specific js for copy button and email link.
18
$(document).on('turbolinks:load', function(){
19
  var controller = $("body").data('controller');
20
  var action = $("body").data('action');
21
22
  // Only run on room pages.
23
  if (controller == "rooms" && action == "show"){
24
    var copy = $('#copy');
25
26
    // Handle copy button.
27
    copy.on('click', function(){
28
      var inviteURL = $('#invite-url');
29
      inviteURL.select();
30
31
      var success = document.execCommand("copy");
32
      if (success) {
33
        inviteURL.blur();
34
        copy.addClass('btn-success');
35
        copy.html("<i class='fas fa-check'></i>" + getLocalizedString("copied"))
36
        setTimeout(function(){
37
          copy.removeClass('btn-success');
38
          copy.html("<i class='fas fa-copy'></i>" + getLocalizedString("copy"))
39
        }, 2000)
40
      }
41
    });
42
43
    // Forces the wrapper to take the entire screen height if the user can't create rooms
44
    if ($("#cant-create-room-wrapper").length){
45
      $(".wrapper").css('height', '100%').css('height', '-=130px');
46
    }
47
48
    // Display and update all fields related to creating a room in the createRoomModal
49
    $("#create-room-block").click(function(){
50
      showCreateRoom(this)
51
    })
52
  }
53
54
    // Autofocus on the Room Name label when creating a room only
55
  $('#createRoomModal').on('shown.bs.modal', function (){
56
    if ($(".create-only").css("display") == "block"){
57
      $('#create-room-name').focus()
58
    }
59
  })
60
  
61
  if (controller == "rooms" && action == "show" || controller == "admins" && action == "server_rooms"){
62
    // Display and update all fields related to creating a room in the createRoomModal
63
    $(".update-room").click(function(){
64
      showUpdateRoom(this)
65
    })
66
67
    $(".delete-room").click(function() {
68
      showDeleteRoom(this)
69
    })
70
71
    $('.selectpicker').selectpicker({
72
      liveSearchPlaceholder: getLocalizedString('javascript.search.start')
73
    });
74
    // Fixes turbolinks issue with bootstrap select
75
    $(window).trigger('load.bs.select.data-api');
76
77
    $(".share-room").click(function() {
78
      // Update the path of save button
79
      $("#save-access").attr("data-path", $(this).data("path"))
80
81
      // Get list of users shared with and display them
82
      displaySharedUsers($(this).data("users-path"))
83
    })
84
85
    $("#shareRoomModal").on("show.bs.modal", function() {
86
      $(".selectpicker").selectpicker('val','')
87
    })
88
89
    $(".bootstrap-select").on("click", function() {
90
      $(".bs-searchbox").siblings().hide()
91
    })
92
93
    $(".bs-searchbox input").on("input", function() {
94
      if ($(".bs-searchbox input").val() == '' || $(".bs-searchbox input").val().length < 3) {
95
        $(".bs-searchbox").siblings().hide()
96
      } else {
97
        $(".bs-searchbox").siblings().show()
98
      }
99
    })
100
101
    $(".remove-share-room").click(function() {
102
      $("#remove-shared-confirm").parent().attr("action", $(this).data("path"))
103
    })
104
105
    // User selects an option from the Room Access dropdown
106
    $(".bootstrap-select").on("changed.bs.select", function(){
107
      // Get the uid of the selected user
108
      let uid = $(".selectpicker").selectpicker('val')
109
110
      // If the value was changed to blank, ignore it
111
      if (uid == "") return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
112
113
      let currentListItems = $("#user-list li").toArray().map(user => $(user).data("uid"))
114
115
      // Check to make sure that the user is not already there
116
      if (!currentListItems.includes(uid)) {
117
        // Create the faded list item and display it
118
        let option = $("option[value='" + uid + "']")
119
120
        let listItem = document.createElement("li")
121
        listItem.setAttribute('class', 'list-group-item text-left not-saved add-access');
122
        listItem.setAttribute("data-uid", uid)
123
  
124
        let spanItem = "<span class='avatar float-left mr-2'>" + option.text().charAt(0) + "</span> <span class='shared-user'>" +
125
          option.text() + " <span class='text-muted'>" + option.data("subtext") + "</span></span>" + 
126
          "<span class='text-primary float-right shared-user cursor-pointer' onclick='removeSharedUser(this)'><i class='fas fa-times'></i></span>"
127
        
128
        listItem.innerHTML = spanItem
129
  
130
        $("#user-list").append(listItem)
131
      }
132
    })
133
  }
134
});
135
136
function showCreateRoom(target) {
137
  var modal = $(target)
0 ignored issues
show
Unused Code introduced by
The variable modal seems to be never used. Consider removing it.
Loading history...
138
  $("#create-room-name").val("")
139
  $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
140
  $("#room_access_code").val(null)
141
142
  $("#createRoomModal form").attr("action", $("body").data('relative-root'))
143
  $("#room_mute_on_join").prop("checked", false)
144
  $("#room_require_moderator_approval").prop("checked", false)
145
  $("#room_anyone_can_start").prop("checked", false)
146
  $("#room_all_join_moderator").prop("checked", false)
147
148
  //show all elements & their children with a create-only class
149
  $(".create-only").each(function() {
150
    $(this).show()
151
    if($(this).children().length > 0) { $(this).children().show() }
152
  })
153
154
  //hide all elements & their children with a update-only class
155
  $(".update-only").each(function() {
156
    $(this).attr('style',"display:none !important")
157
    if($(this).children().length > 0) { $(this).children().attr('style',"display:none !important") }
158
  })
159
}
160
161
function showUpdateRoom(target) {
162
  var modal = $(target)
163
  var update_path = modal.closest("#room-block").data("path")
164
  var settings_path = modal.data("settings-path")
165
  $("#create-room-name").val(modal.closest("#room-block").find("#room-name-text").text())
166
  $("#createRoomModal form").attr("action", update_path)
167
168
  //show all elements & their children with a update-only class
169
  $(".update-only").each(function() {
170
    $(this).show()
171
    if($(this).children().length > 0) { $(this).children().show() }
172
  })
173
174
  //hide all elements & their children with a create-only class
175
  $(".create-only").each(function() {
176
    $(this).attr('style',"display:none !important")
177
    if($(this).children().length > 0) { $(this).children().attr('style',"display:none !important") }
178
  })
179
180
  updateCurrentSettings(settings_path)
181
  
182
  var accessCode = modal.closest("#room-block").data("room-access-code")
183
184
  if(accessCode){
185
    $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code") + ": " + accessCode)
186
    $("#room_access_code").val(accessCode)
187
  } else {
188
    $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
189
    $("#room_access_code").val(null)
190
  }
191
}
192
193
function showDeleteRoom(target) {
194
  $("#delete-header").text(getLocalizedString("modal.delete_room.confirm").replace("%{room}", $(target).data("name")))
195
  $("#delete-confirm").parent().attr("action", $(target).data("path"))
196
}
197
198
//Update the createRoomModal to show the correct current settings
199
function updateCurrentSettings(settings_path){
200
  // Get current room settings and set checkbox
201
  $.get(settings_path, function(room_settings) {
202
    var settings = JSON.parse(room_settings) 
203
    $("#room_mute_on_join").prop("checked", settings.muteOnStart)
204
    $("#room_require_moderator_approval").prop("checked", settings.requireModeratorApproval)
205
    $("#room_anyone_can_start").prop("checked", settings.anyoneCanStart)
206
    $("#room_all_join_moderator").prop("checked", settings.joinModerator)
207
  })
208
}
209
210
function generateAccessCode(){
211
  const accessCodeLength = 6
212
  var validCharacters = "0123456789"
213
  var accessCode = ""
214
215
  for( var i = 0; i < accessCodeLength; i++){
216
    accessCode += validCharacters.charAt(Math.floor(Math.random() * validCharacters.length));
217
  }
218
219
  $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code") + ": " + accessCode)
220
  $("#room_access_code").val(accessCode)
221
}
222
223
function ResetAccessCode(){
224
  $("#create-room-access-code").text(getLocalizedString("modal.create_room.access_code_placeholder"))
225
  $("#room_access_code").val(null)
226
}
227
228
function saveAccessChanges() {
229
  let listItemsToAdd = $("#user-list li:not(.remove-shared)").toArray().map(user => $(user).data("uid"))
230
231
  $.post($("#save-access").data("path"), {add: listItemsToAdd})
232
}
233
234
// Get list of users shared with and display them
235
function displaySharedUsers(path) {
236
  $.get(path, function(users) {
237
    // Create list element and add to user list
238
    var user_list_html = ""
239
240
    users.forEach(function(user) {
241
      user_list_html += "<li class='list-group-item text-left' data-uid='" + user.uid + "'>"
242
243
      if (user.image) {
244
        user_list_html += "<img id='user-image' class='avatar float-left mr-2' src='" + user.image + "'></img>"
245
      } else {
246
        user_list_html += "<span class='avatar float-left mr-2'>" + user.name.charAt(0) + "</span>"
247
      }
248
      user_list_html += "<span class='shared-user'>" + user.name + "<span class='text-muted ml-1'>" + user.uid + "</span></span>"
249
      user_list_html += "<span class='text-primary float-right shared-user cursor-pointer' onclick='removeSharedUser(this)'><i class='fas fa-times'></i></span>"
250
      user_list_html += "</li>"
251
    })
252
    
253
    $("#user-list").html(user_list_html)
254
  });
255
}
256
257
// Removes the user from the list of shared users
258
function removeSharedUser(target) {
259
  let parentLI = target.closest("li")
260
261
  if (parentLI.classList.contains("not-saved")) {
262
    parentLI.parentNode.removeChild(parentLI)
263
  } else {
264
    parentLI.removeChild(target)
265
    parentLI.classList.add("remove-shared")
266
  }
267
}